package cn.tedu.antconlonysever.contract.controller;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.JsonResult;
import cn.tedu.antconlonysever.contract.pojo.param.ContactAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.ContactUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.param.ContractAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.ContractUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.ContactListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.ContactStandardVO;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractListVo;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractStandardVO;
import cn.tedu.antconlonysever.contract.service.IContactService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/contact")
@Api(tags = "06.会面管理")
@Slf4j
@Validated
public class ContactController {
    @Autowired
    private IContactService contactService;

    @PostMapping("/add-New")
    @ApiOperation("添加会面")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid ContactAddNewParam contactAddNewParam) {
        log.debug("开始处理【添加会面】的请求，参数：{}", contactAddNewParam);
        contactService.addNew(contactAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除会面")
    @PreAuthorize("hasAuthority('/pms/contact/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会面ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "根据ID删除会面失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID删除会面】的请求，参数：{}", id);
        contactService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改会面")
    @PreAuthorize("hasAuthority('/pms/contact/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会面ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的会面ID值!") Long id,
                                           @Valid ContactUpdateParam contactUpdateParam) {
        log.debug("开始处理【修改会面】的请求，新数据：{}", contactUpdateParam);
        contactService.updateInfoById(id, contactUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询会面详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同id", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "根据id查询会面详情失败！请输入合法的数据参数，") Long id) {
        log.debug("开始处理【根据ID查询会面详情】的请求，iD：{}，", id);
        ContactStandardVO result = contactService.getStandardById(id);
        return JsonResult.ok(result);

    }

    @GetMapping("")
    @ApiOperation("查询会面列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<ContactListVO>> list(@Range(min = 1, message = "查询会面列表失败，请提供正确的页码值！") Integer page,
                                                    String queryType) {
        log.debug("开始处理【查询会面列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<ContactListVO> pageData;
        if ("all".equals(queryType)) {
            pageData = contactService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = contactService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

}
